package com.eternalrights.service.impl;

import com.eternalrights.mapper.StudentMapper;
import com.eternalrights.pojo.PageResult;
import com.eternalrights.pojo.Student;
import com.eternalrights.pojo.StudentQueryReuslt;
import com.eternalrights.pojo.ViolationData;
import com.eternalrights.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentSerivceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> page(StudentQueryReuslt studentQueryReuslt) {
        PageHelper.startPage(studentQueryReuslt.getPage(),studentQueryReuslt.getPageSize());
        List<Student> list = studentMapper.list(studentQueryReuslt);
        Page<Student> p = (Page) list;
        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public Student getInFo(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        studentMapper.updateUpdateTime(LocalDateTime.now(),id);
        ViolationData vd = studentMapper.countViolation(id);
        studentMapper.updateViolation(vd.getCount()+1,vd.getScore()+score,id);
    }
}